package com.car.merge.fun.sdk;

import android.util.Log;

import com.car.merge.fun.sdk.ad.CarMFVideoRewardCallback;
import com.car.merge.fun.sdk.ad.CarMFVideoIAP;
import com.car.merge.fun.sdk.bridge.CarMFCocosBridge;
import com.car.merge.fun.sdk.ad.CarMFVideoManger;
import com.car.merge.fun.sdk.ad.CarMFMaxReward;

import org.json.JSONObject;

public class CarMFAdSdk {

    public static void showBiddingAd(String ad_source) {

        CarMFVideoIAP service = getAdService();

        if (service == null) {
            CarMFCocosBridge.getInstance().XSSdkCallback("ad_error_wh", "{}");
            return;
        }

        try {
            service.showVideo(CarMFCocosBridge.mainGameActivity.get(), new CarMFVideoRewardCallback() {
                @Override
                public void onAdOver() {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("ad_source",ad_source);
                        CarMFCocosBridge.getInstance().XSSdkCallback("ad_over_wh", jsonObject.toString());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onAdError() {
                    CarMFCocosBridge.getInstance().XSSdkCallback("ad_error_wh", "{}");
                }
            });
        } catch (Exception e) {
            try {
                CarMFCocosBridge.getInstance().XSSdkCallback("ad_error_wh", "{}");
            } catch (Exception e1) {
            }
        }
    }

    private static CarMFVideoIAP getAdService() {
        CarMFVideoIAP service = null;
        boolean isMaxReady =  CarMFMaxReward.getInstance().isReady();
        if (isMaxReady){
            Log.e("CarWh","show max reward");
            service= CarMFVideoManger.getReward1Instance();
        }
        return service;
    }

}
